package com.hskn.hss.module.costdictitem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.costdict.entity.CostDict;
import com.hskn.hss.module.costdictitem.entity.CostDictItem;
import com.hskn.hss.module.costdictitem.mapper.CostDictItemMapper;
import com.hskn.hss.module.costdictitem.service.ICostDictItemService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.GroupingByUtils;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-05
 */
@Service
public class CostDictItemServiceImpl extends ServiceImpl<CostDictItemMapper, CostDictItem> implements ICostDictItemService {

    @Resource
    CostDictItemMapper CostDictItemMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(JSONObject params) {

        String uid = LoginUtils.getUser().getUid();
        CostDictItem costDictItem = params.getObject("data", CostDictItem.class);
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        costDictItem.setId(StringUtils.uuid());
        costDictItem.setCreatby(uid);
        costDictItem.setDeptid(deptid);
        int i = CostDictItemMapper.insert(costDictItem);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) {
        CostDictItem costDictItem = params.getObject("data", CostDictItem.class);
        int i = CostDictItemMapper.updateById(costDictItem);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(String id) {
        try {
            CostDictItemMapper.deleteById(id);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public CostDictItem getitemByid(String id) throws HssException {
        QueryWrapper<CostDictItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CostDictItem::getId, id);
        CostDictItem costDictItem = CostDictItemMapper.selectOne(queryWrapper);
        return costDictItem;
    }

    @Override
    public JSONObject getitemList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map map = new HashMap();
        map.put("dictid", params.getString("dictid"));

        Page<CostDictItem> detailPage = new Page<>(page, size, true);
        IPage<CostDictItem> iPage = CostDictItemMapper.listdetail(detailPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("detallist", iPage.getRecords());
        return json;
    }

    @Override
    public List getSelectList(JSONObject params) {
        Map map = new HashMap();
        map.put("dictid", params.getString("dictid"));
        List resultList = CostDictItemMapper.getSelectList(map);
        return resultList;
    }
    @Override
    public Map<String, CostDictItem> getCostDictItemMap(QueryWrapper<CostDictItem> queryWrapper){
        List<CostDictItem> costDictItemList = CostDictItemMapper.selectList(queryWrapper);
        Map<String ,CostDictItem> stringCostDictItemMap=GroupingByUtils.getGroupingForMapEntity(costDictItemList,CostDictItem::getId, Collectors.collectingAndThen(Collectors.toList(), value -> value.get(0)));
        return stringCostDictItemMap;
    }
}
